package my.test.threads;

/**
 * This program shows how to start threads in sequence;
 * 1) one is way to use join()
 * 2) if not other way would be to start threads in a way where one notifies
 * second which then notifies third & so on for start
 * 
 * @author LalitR
 */
public final class ThreadsInSequence {
	
	public static void main(String[] args) {
		MyThread mythread = null;
		Thread t1 = null;
		
		for (int i = 0; i <= 3; i++) {
			mythread = new MyThread(i);
			t1 = new Thread(mythread);
			t1.start();
			try {
				t1.join(); // current thread waits for t1 to finish
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("Thread " + i + " done");
		}
		
		
		mythread = new MyThread(4);
		t1 = new Thread(mythread);
		t1.start();
		try {
			t1.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("Thread 4 done");
		
		mythread = new MyThread(5);
		Thread t2 = new Thread(mythread);
		t2.start();
		try {
			t2.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("Thread 5 done");
	}
	
}
